﻿using System.Web.Mvc;
using Common;
using Common.Factory;
using Common.Model;

namespace school.Common.Base
{
    public class ManagerController : Controller
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (GetSessionManagerInfo() == null)
            {
                filterContext.Result = RedirectToAction("Login", "Common", new { Areas = "Manager", backUrl = Request.RawUrl });
                return;
            }
            base.OnActionExecuting(filterContext);
        }

        public Cd_User UserContext
        {
            get { return GetSessionManagerInfo(); }
        }

        public Cd_User GetSessionManagerInfo()
        {
            Cd_User manager = Session[Key.MANAGER_INFO] as Cd_User;
            if (manager == null)
            {
                string username = Utils.GetCookie(Key.MANAGER_NAME);
                string password = Utils.GetCookie(Key.MANAGER_PASS);
                if (username != "" || password != "")
                {
                    manager = Factory.UserService().Login(username, password);
                    if (manager != null)
                    {
                        Session[Key.MANAGER_INFO] = manager;
                        return manager;
                    }
                }
                return null;
            }
            return manager;
        }
    }
}